package com.lwx.bus.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lwx.bus.domain.Provider;
import com.lwx.bus.mapper.ProviderMapper;
import com.lwx.bus.service.ProviderService;
import com.lwx.sys.constant.Constant;
import com.lwx.sys.utils.DataGridView;
import com.lwx.sys.vo.ProviderVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author cn.lwx
 * @since 2020-04-21
 */
@Service
public class ProviderServiceImpl extends ServiceImpl<ProviderMapper, Provider> implements ProviderService {
    @Autowired
    private ProviderMapper providerMapper;

    @Override
    public DataGridView loadAllProvider(ProviderVo providerVo) {
        QueryWrapper<Provider> qw = new QueryWrapper<>();
        qw.like(StringUtils.isNotBlank(providerVo.getProvidername()),"providername",providerVo.getProvidername());
        qw.like(StringUtils.isNotBlank(providerVo.getTelephone()),"telephone",providerVo.getTelephone());
        qw.like(StringUtils.isNotBlank(providerVo.getConnectionperson()),"connectionperson",providerVo.getConnectionperson());
        //分页
        Page page = new Page<>(providerVo.getPage(), providerVo.getLimit());

        //查询数据库
        providerMapper.selectPage(page,qw);

        return new DataGridView(page.getTotal(),page.getRecords());
    }

    //增删改都要加事务
    @Transactional
    @Override
    public void deleteProvider(Integer id) {

        providerMapper.deleteById(id);
    }

    @Transactional
    @Override
    public void batchDeleteProvider(Integer[] ids) {
        providerMapper.deleteBatchIds(Arrays.asList(ids));
    }

    @Transactional
    @Override
    public void addProvider(Provider provider) {
        providerMapper.insert(provider);
    }

    @Transactional
    @Override
    public void updateProvider(Provider provider) {

        providerMapper.updateById(provider);
    }

    @Override
    public Provider selectProviderById(Integer providerid) {
        return providerMapper.selectById(providerid);
    }

    @Override
    public DataGridView queryAllAvailableProvider() {

        QueryWrapper<Provider> wrapper = new QueryWrapper<>();
        wrapper.eq("available", Constant.AVAILABLE_TRUE);
        List<Provider> providers = providerMapper.selectList(wrapper);
        return new DataGridView(providers);
    }
}
